﻿using Infrastructure.ElasticSearch.ParentChilds;
using Nest;
using System;
using System.Linq.Expressions;

namespace Operation.Marketing.ElasticSearch.Query.BaseSpecifications
{
    public class StringMatchSpecification<T> : ISearchSpecification<ISearchRequest<T>> where T : class
    {
        private readonly string _value;
        private readonly Expression<Func<T, string>> _field;

        public StringMatchSpecification(string value, Expression<Func<T, string>> field)
        {
            _value = value;
            _field = field;
        }
        public ISearchRequest<T> IsSatisfiedBy()
        {
            return new SearchDescriptor<T>()
                .Query(q => q
                    .Match(m => m
                        .Field(_field)
                        .Query(_value)
                    )
                );
        }
    }
}